﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;



public class Enum_Helper
{
    public static string GetString(GroupFields groupfields)
    {
        StringBuilder sb = new StringBuilder();
        if ((groupfields & GroupFields.DistributeRate) == GroupFields.DistributeRate)
        {
            sb.AppendFormat(",{0} AS DistributeRate", EnumMapHelper.GetStringFromEnum(GroupFields.DistributeRate));
        }

        if ((groupfields & GroupFields.PriceAvg) == GroupFields.PriceAvg)
        {
            sb.AppendFormat(",{0} AS PriceAvg", EnumMapHelper.GetStringFromEnum(GroupFields.PriceAvg));
        }
        if ((groupfields & GroupFields.PurchaseAmountTotal) == GroupFields.PurchaseAmountTotal)
        {
            sb.AppendFormat(",{0} AS PurchaseAmountTotal", EnumMapHelper.GetStringFromEnum(GroupFields.PurchaseAmountTotal));
        }
        if ((groupfields & GroupFields.PurchaseCountTotal) == GroupFields.PurchaseCountTotal)
        {
            sb.AppendFormat(",{0} AS PurchaseCountTotal", EnumMapHelper.GetStringFromEnum(GroupFields.PurchaseCountTotal));
        }

        if ((groupfields & GroupFields.DistributeCountTotal) == GroupFields.DistributeCountTotal)
        {
            sb.AppendFormat(",{0} AS DistributeCountTotal", EnumMapHelper.GetStringFromEnum(GroupFields.DistributeCountTotal));
        }
        if ((groupfields & GroupFields.WareHouseCountTotal) == GroupFields.WareHouseCountTotal)
        {
            sb.AppendFormat(",{0} AS WareHouseCountTotal", EnumMapHelper.GetStringFromEnum(GroupFields.WareHouseCountTotal));
        }
        if ((groupfields & GroupFields.WareHouseRate) == GroupFields.WareHouseRate)
        {
            sb.AppendFormat(",{0} AS WareHouseRate", EnumMapHelper.GetStringFromEnum(GroupFields.WareHouseRate));
        }
        if ((groupfields & GroupFields.WareHouseAmountTotal) == GroupFields.WareHouseAmountTotal)
        {
            sb.AppendFormat(",{0} AS WareHouseAmountTotal", EnumMapHelper.GetStringFromEnum(GroupFields.WareHouseAmountTotal));
        }
        if ((groupfields & GroupFields.CountOrder) == GroupFields.CountOrder)
        {
            sb.AppendFormat(",{0} AS CountOrder", EnumMapHelper.GetStringFromEnum(GroupFields.CountOrder));
        }
        if ((groupfields & GroupFields.DistributeAmountTotal) == GroupFields.DistributeAmountTotal)
        {
            sb.AppendFormat(",{0} AS DistributeAmountTotal", EnumMapHelper.GetStringFromEnum(GroupFields.DistributeAmountTotal));
        }
        if ((groupfields & GroupFields.DecreasingAmplitudeBatch) == GroupFields.DecreasingAmplitudeBatch)
        {
            sb.AppendFormat(",{0} AS DecreasingAmplitudeBatch", EnumMapHelper.GetStringFromEnum(GroupFields.DecreasingAmplitudeBatch));
        }
        if ((groupfields & GroupFields.DrugCount) == GroupFields.DrugCount)
        {
            sb.AppendFormat(",{0} AS DrugCount", EnumMapHelper.GetStringFromEnum(GroupFields.DrugCount));
        }
        if ((groupfields & GroupFields.Profit) == GroupFields.Profit)
        {
            sb.AppendFormat(",{0} AS Profit", EnumMapHelper.GetStringFromEnum(GroupFields.Profit));
        }

        if ((groupfields & GroupFields.ReturnedGoods) == GroupFields.ReturnedGoods)
        {
            sb.AppendFormat(",{0} AS ReturnedGoods", EnumMapHelper.GetStringFromEnum(GroupFields.ReturnedGoods));
        }

        if ((groupfields & GroupFields.PayOffAmount) == GroupFields.PayOffAmount)
        {
            sb.AppendFormat(",{0} AS PayOffAmount", EnumMapHelper.GetStringFromEnum(GroupFields.PayOffAmount));
        }
        return sb.ToString();
    }

    /// <summary>
    /// 根据枚举值获取转换结果
    /// </summary>
    /// <param name="gbfield"></param>
    /// <param name="flags">是否直接使用变量，true不加入AS，false加入AS</param>
    /// <returns></returns>
    public static string GetString(GroupByField gbfield, bool flags)
    {
        StringBuilder sb = new StringBuilder();
        Type type = typeof(GroupByField);
        FieldInfo[] fields = type.GetFields();

        int value = (int)gbfield;
        bool first = true;
        foreach (var item in fields)
        {
            if (item.FieldType == typeof(int))
                continue;

            object obj = item.GetValue(gbfield);

            if (((GroupByField)obj & gbfield) == (GroupByField)obj)
            {
                object[] objs = item.GetCustomAttributes(false);
                string des = string.Empty;
                if (objs.Length > 0)
                {
                    des = (objs[0] as EnumAttribute).Expression;

                }
                else
                {
                    des = obj.ToString();
                }
                if (flags)
                {
                    if (first)
                    {
                        sb.AppendFormat("{0}", des, obj);
                        first = false;
                    }
                    else
                    {
                        sb.AppendFormat(",{0}", des, obj);
                    }

                }
                else
                {
                    if (first)
                    {

                        sb.AppendFormat("{0} AS {1}", des, obj);

                        first = false;
                    }
                    else
                    {
                        sb.AppendFormat(",{0} AS {1}", des, obj);
                    }
                }
            }

        }



        return sb.ToString();
    }
}